package com.gimi.cloud.bbp.dao.dao.prod;

import com.gimi.cloud.bbp.dao.dao.sys.BaseDao;
import com.gimi.cloud.bbp.dao.myMapper.MyMapper;
import com.gimi.cloud.bbp.dto.prod.EcProdCatModelDTO;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
*  商品模版属性对应dao
* @author loongs
* @Description
* @create 2017-12-18
* @version 1.0
**/
@Mapper
public interface EcProdCatModelDao extends MyMapper<EcProdCatModelEntity>,BaseDao<EcProdCatModelEntity,EcProdCatModelDTO> {

    /**
     * 通过prodCatId 与prodPropName查询不在指定ModelId中的总数
     * @param prodCatId 品类id
     * @param prodPropName 品类属性名称
     * @param modelId 品类属性id
     * @return 查询总数
     * @throws Exception
     */
    @Select("SELECT count(*) FROM ec_prod_cat_model WHERE is_delete = 0 AND prod_cat_id =#{prodCatId}  AND prod_prop_name = #{prodPropName} AND model_id != IFNULL(#{modelId},-1)")
    int selectCountByCatIdAndPropNameExcludeModelId(@Param("prodCatId")String prodCatId,@Param("prodPropName") String prodPropName,@Param("modelId")Integer modelId) throws Exception;

    /**
     * 根据品类id获取模版属性集合
     * @param prodCatId 品类id
     * @return 模版属性集合
     * @throws Exception
     */
    @Select("SELECT * FROM ec_prod_cat_model WHERE is_delete = 0 and prod_cat_id = #{prodCatId}")
    List<EcProdCatModelDTO> queryEcprodCatModelByCatId(@Param("prodCatId") String prodCatId) throws Exception;

    /**
     * 根据品类ID获取模板属性值
     * @param prodCatId
     * @return
     */
    @Select("SELECT modelv.cm_id,model.prod_prop_name,modelv.model_value,modelv.is_img,seq, model.model_id,model.prod_cat_id" +
            " from ec_prod_cat_model model " +
            " LEFT JOIN ec_prod_cat_model_value modelv on modelv.is_delete = 0 AND modelv.model_id = model.model_id" +
            " where model.is_delete = 0 AND model.prod_cat_id = #{prodCatId}")
    List<EcProdCatModelDTO> getCatModelByCatId(String prodCatId);

    @Select("select * from ec_prod_cat_model epcm where epcm.prod_cat_id in " +
            "(select DISTINCT  ep.prod_cat_id from ec_prod_status eps LEFT JOIN ec_product ep on eps.prod_id =ep.prod_id where prod_status =1) " +
            "and epcm.model_id=#{modelId}")
    EcProdCatModelEntity selectProductIsPublished(@Param("modelId") Integer modelId);


    @Select("select epcm.* from ec_prod_cat_model epcm LEFT JOIN ec_prod_cat_model_value epcmv on epcm.model_id=epcmv.model_id where epcmv.cm_id=#{cmId}")
    EcProdCatModelEntity selectModelByCmId(@Param("cmId") Integer cmId);

    @Select("select * from ec_prod_cat_model where prod_cat_id=#{prodCatId} AND is_delete = 0")
    List<EcProdCatModelEntity> findCatModelByCatId(@Param(value = "prodCatId") String prodCatId);
}
